﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.Common;

using Microsoft.Practices.EnterpriseLibrary.Data;

using MyEntLibCMS.IDAL;
using MyEntLibCMS.Model;
using MyEntLibCMS.Utility;

namespace MyEntLibCMS.DAL
{
    public class LoginRepository : ILoginRepository
    {
        /// <summary>
        /// 登录系统
        /// </summary>
        /// <param name="loginAccount">账号</param>
        /// <param name="password">密码</param>
        /// <returns>登录信息</returns>
        public Login Login(string loginAccount, string password)
        {
            Login login = null;
            Database db = DBHelper.CreateDataBase();
            StringBuilder sql = new StringBuilder();
            sql.Append("select * from Login where LoginAccount=@LoginAccount and Password=@Password and Status=1 ");
            DbCommand cmd = db.GetSqlStringCommand(sql.ToString());
            db.AddInParameter(cmd, "@LoginAccount", DbType.String, loginAccount);
            db.AddInParameter(cmd, "@Password", DbType.String, password);
            using (IDataReader reader = db.ExecuteReader(cmd))
            {
                if (reader.Read())
                {
                    login = new Login()
                    {
                        LoginAccount = (string)reader["LoginAccount"],
                        LoginID = (int)reader["LoginID"],
                        LoginTime = DateTime.Now,
                        Password = (string)reader["Password"],
                        Status = (bool)reader["Status"],
                        UserID = (int)reader["UserID"]
                    };
                }
            }
            return login;
        }
        /// <summary>
        /// 修改密码
        /// </summary>
        /// <param name="loginAccount">账号</param>
        /// <param name="newPassword">新密码</param>
        /// <returns></returns>
        public bool UpdatePassword(string loginAccount, string newPassword)
        {
            Database db = DBHelper.CreateDataBase();
            StringBuilder sql = new StringBuilder();
            sql.Append("update Login set Password=@Password where LoginAccount=@LoginAccount");
            DbCommand cmd = db.GetSqlStringCommand(sql.ToString());
            db.AddInParameter(cmd, "@Password", DbType.String, newPassword);
            db.AddInParameter(cmd, "@LoginAccount", DbType.String, loginAccount);
            return db.ExecuteNonQuery(cmd) > 0 ? true : false;
        }

        /// <summary>
        /// 修改登录IP,时间
        /// </summary>
        /// <param name="userID">用户ID</param>
        /// <param name="loginIP">IP</param>
        /// <param name="loginTime">登录时间</param>
        /// <returns></returns>
        public bool UpdateLoginIP(string userID, string loginIP, DateTime loginTime)
        {
            Database db = DBHelper.CreateDataBase();
            StringBuilder sql = new StringBuilder();
            sql.Append("update Login set LoginTime=@LoginTime,LoginIP=@LoginIP where UserID=@UserID");
            DbCommand cmd = db.GetSqlStringCommand(sql.ToString());
            db.AddInParameter(cmd, "@LoginTime", DbType.DateTime, loginTime);
            db.AddInParameter(cmd, "@UserID", DbType.Int32, userID);
            db.AddInParameter(cmd, "@LoginIP", DbType.String, loginIP);
            return db.ExecuteNonQuery(cmd) > 0 ? true : false;
        }
    }
}